package com.xteam.xnetworkdisk.utils;

import com.xteam.xnetworkdisk.constant.FIleTypeConstant;

import java.util.HashMap;
import java.util.Map;

public class    FileTypeUtils {

    private static final Map<String, Integer> MIME_TYPE_TO_CATEGORY = new HashMap<>();
    private static final Map<String, String> EXTENSION_TO_MIME_TYPE = new HashMap<>();

    static {
        // 图片
        MIME_TYPE_TO_CATEGORY.put("image/jpeg", FIleTypeConstant.IMAGE);
        MIME_TYPE_TO_CATEGORY.put("image/png", FIleTypeConstant.IMAGE);
        MIME_TYPE_TO_CATEGORY.put("image/gif", FIleTypeConstant.IMAGE);
        MIME_TYPE_TO_CATEGORY.put("image/bmp", FIleTypeConstant.IMAGE);
        MIME_TYPE_TO_CATEGORY.put("image/webp", FIleTypeConstant.IMAGE);
        MIME_TYPE_TO_CATEGORY.put("image/svg+xml", FIleTypeConstant.IMAGE);

        // 视频
        MIME_TYPE_TO_CATEGORY.put("video/mp4", FIleTypeConstant.VIDEO);
        MIME_TYPE_TO_CATEGORY.put("video/avi", FIleTypeConstant.VIDEO);
        MIME_TYPE_TO_CATEGORY.put("video/mkv", FIleTypeConstant.VIDEO);
        MIME_TYPE_TO_CATEGORY.put("video/x-msvideo", FIleTypeConstant.VIDEO);
        MIME_TYPE_TO_CATEGORY.put("video/quicktime", FIleTypeConstant.VIDEO);

        // 音频
        MIME_TYPE_TO_CATEGORY.put("audio/mpeg", FIleTypeConstant.AUDIO);
        MIME_TYPE_TO_CATEGORY.put("audio/wav", FIleTypeConstant.AUDIO);
        MIME_TYPE_TO_CATEGORY.put("audio/ogg", FIleTypeConstant.AUDIO);
        MIME_TYPE_TO_CATEGORY.put("audio/aac", FIleTypeConstant.AUDIO);

        // 文档
        MIME_TYPE_TO_CATEGORY.put("application/pdf", FIleTypeConstant.DOCUMENT);
        MIME_TYPE_TO_CATEGORY.put("application/msword", FIleTypeConstant.DOCUMENT); // .doc
        MIME_TYPE_TO_CATEGORY.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", FIleTypeConstant.DOCUMENT); // .docx
        MIME_TYPE_TO_CATEGORY.put("application/vnd.ms-excel", FIleTypeConstant.DOCUMENT); // .xls
        MIME_TYPE_TO_CATEGORY.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", FIleTypeConstant.DOCUMENT); // .xlsx
        MIME_TYPE_TO_CATEGORY.put("application/epub+zip", FIleTypeConstant.DOCUMENT); // EPUB
        MIME_TYPE_TO_CATEGORY.put("application/zip", FIleTypeConstant.DOCUMENT); // ZIP 文件
        MIME_TYPE_TO_CATEGORY.put("application/x-rar-compressed", FIleTypeConstant.DOCUMENT); // RAR 文件
        MIME_TYPE_TO_CATEGORY.put("text/plain", FIleTypeConstant.DOCUMENT); // .txt
        MIME_TYPE_TO_CATEGORY.put("text/csv", FIleTypeConstant.DOCUMENT); // .csv

        // 其他
        MIME_TYPE_TO_CATEGORY.put("application/octet-stream", FIleTypeConstant.OTHER);
        MIME_TYPE_TO_CATEGORY.put("text/html", FIleTypeConstant.OTHER); // HTML 文件
        // 添加更多 MIME 类型和类别的映射

        // 文件扩展名到 MIME 类型的映射
        EXTENSION_TO_MIME_TYPE.put("jpg", "image/jpeg");
        EXTENSION_TO_MIME_TYPE.put("jpeg", "image/jpeg");
        EXTENSION_TO_MIME_TYPE.put("png", "image/png");
        EXTENSION_TO_MIME_TYPE.put("gif", "image/gif");
        EXTENSION_TO_MIME_TYPE.put("bmp", "image/bmp");
        EXTENSION_TO_MIME_TYPE.put("webp", "image/webp");
        EXTENSION_TO_MIME_TYPE.put("svg", "image/svg+xml");

        EXTENSION_TO_MIME_TYPE.put("mp4", "video/mp4");
        EXTENSION_TO_MIME_TYPE.put("avi", "video/avi");
        EXTENSION_TO_MIME_TYPE.put("mkv", "video/mkv");
        EXTENSION_TO_MIME_TYPE.put("mov", "video/quicktime");

        EXTENSION_TO_MIME_TYPE.put("mp3", "audio/mpeg");
        EXTENSION_TO_MIME_TYPE.put("wav", "audio/wav");
        EXTENSION_TO_MIME_TYPE.put("ogg", "audio/ogg");
        EXTENSION_TO_MIME_TYPE.put("aac", "audio/aac");

        EXTENSION_TO_MIME_TYPE.put("pdf", "application/pdf");
        EXTENSION_TO_MIME_TYPE.put("doc", "application/msword");
        EXTENSION_TO_MIME_TYPE.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        EXTENSION_TO_MIME_TYPE.put("xls", "application/vnd.ms-excel");
        EXTENSION_TO_MIME_TYPE.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        EXTENSION_TO_MIME_TYPE.put("epub", "application/epub+zip");
        EXTENSION_TO_MIME_TYPE.put("zip", "application/zip");
        EXTENSION_TO_MIME_TYPE.put("rar", "application/x-rar-compressed");
        EXTENSION_TO_MIME_TYPE.put("txt", "text/plain");
        EXTENSION_TO_MIME_TYPE.put("csv", "text/csv");

        EXTENSION_TO_MIME_TYPE.put("html", "text/html");
    }

    public static int getFileType(String contentType) {
        return MIME_TYPE_TO_CATEGORY.getOrDefault(contentType, FIleTypeConstant.OTHER);
    }

    public static int getFileTypeByExtension(String fileExtension) {
        String contentType = EXTENSION_TO_MIME_TYPE.get(fileExtension.toLowerCase());
        if (contentType != null) {
            return getFileType(contentType);
        }
        return FIleTypeConstant.OTHER;
    }

}
